package com.winning.yanghao.designpattern.singleton.register;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 注册器式单例（使用ConcurrentHashMap线程安全）
 */
public class BeanFactory {

    private BeanFactory(){}

    private static Map<String, Object> ioc = new ConcurrentHashMap<String, Object>();

    public static Object getInstance(String name){
        try {
            Object obj = Class.forName(name).newInstance();

            //如果不存在，则put
            ioc.putIfAbsent(name, obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ioc.get(name);
    }

}
